package interface_client;

import com.desanonymerUtilisateurs.ConnectDatabase;
import com.lloseng.ocsf.simplegui.client.SimpleClient;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class EcouteurCreer implements ActionListener {
    
    ConnectDatabase connect = new ConnectDatabase();
    private SimpleClient simpleClient;
    JRadioButton vpublic;
    JRadioButton vprive;
    int visibility;
    
    public EcouteurCreer(SimpleClient client)
    {
        simpleClient = client;
    }
    
    @Override
    public void actionPerformed(ActionEvent e) 
    {
        final JFrame sondage = new JFrame();            

        JPanel panel= new JPanel();   
        panel.setLayout(new GridLayout(3, 3));

        JLabel question = new JLabel("Entrez votre question : ");
        panel.add(question);

        final JTextArea question_field = new JTextArea();
        panel.add(question_field);


        vprive = new JRadioButton("Privé");
        vpublic = new JRadioButton("Public");

        vprive.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
               if (vprive.isSelected())
                vpublic.setEnabled(false);
               else
                   vpublic.setEnabled(true);
            }
        });

        vpublic.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (vpublic.isSelected())
                    vprive.setEnabled(false);
                else
                    vprive.setEnabled(true);
            }
        });
        
        JButton valider = new JButton("Valider");

        JButton annuler = new JButton("Annuler"); 

        panel.add(vprive);
        panel.add(vpublic);

        panel.add(valider);
        panel.add(annuler);

        sondage.getContentPane().add(panel, BorderLayout.CENTER);

        sondage.setTitle("Creer votre sondage");
        sondage.setSize(600, 400);

        sondage.show();

        valider.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {

                String contenu_area;
                contenu_area = question_field.getText();
                if (vpublic.isSelected())
                    visibility = 1; 
                else if (vprive.isSelected())
                    visibility = 0;
                
                System.out.println("Visibility = " + visibility);
                
                try
                {
                    simpleClient.openConnection();
                    simpleClient.sendToServer("#createSondage " + contenu_area + " " + visibility);
                }
                catch(Exception ex)
                {
                    ex.printStackTrace();
                }


                System.out.println(contenu_area);

                //erreur dans l'execution du do request


                sondage.hide();
            }
        });

        annuler.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
               sondage.hide();
            }
        });
    }
}
